package com.example.springbott.controller;

import com.example.springbott.aspactJ.OperationName;
import com.example.springbott.bean.BaseRespVo;
import com.example.springbott.bean.MarketOrder;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.silic.*;
import com.example.springbott.bean.zeng.AdminOrderReplyBO;
import com.example.springbott.service.AdminOrderService;
import com.example.springbott.util.Count;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.PublicKey;
import java.util.List;


/**
 * 商场管理——订单管理
 *
 * @author silic xia
 * @since 2022/11/21 21:36
 */
@RestController
@RequestMapping("admin/order")
public class AdminOrderController {

    @Autowired
    AdminOrderService adminOrderService;

    /**
     * List
     *
     * @param /page=1&limit=20 &timeArray=   &sort=add_time&order=desc   &orderId=没用
     *                         &orderStatusArray=订单状态 &start=始日期&end=末日期&userId=1&orderSn=订单编号
     * @return BaseRespVo -- CommonData -- MarketOrder
     * @author silic xia
     * @since 2022/11/21 21:38
     */
    @RequiresPermissions("admin:order:list")
    @RequestMapping("list")
    @OperationName("2")
    public BaseRespVo adminOrderList(BasePageInfo info, AdminOrderListBo adminOrderListBo) {
        CommonData<MarketOrder> data = adminOrderService.adminOrderList(info, adminOrderListBo);
        return BaseRespVo.ok(data);
    }

    /**
     * channel
     *
     * @param /null
     * @return code: "FEDEX_GJ"   ship_sn
     * name: "FEDEX联邦(国际件)"  ship_channel
     * errmsg: "成功"
     * errno: 0
     * @author silic xia
     * @since 2022/11/21 22:26
     */
    @RequestMapping("channel")
    @OperationName("2")
    public BaseRespVo adminOrderChannel() {
        return BaseRespVo.ok(Count.data);
    }

    /**
     * 按details查询订单
     *
     * @param /id=1 用like
     * @return
     * @author silic xia
     * @since 2022/11/21 22:40
     */
    @RequiresPermissions("admin:order:read")
    @RequestMapping("detail")
    @OperationName("2")
    public BaseRespVo adminOrderDetail(String id) {
        OrderGoodsWithUserVo data = adminOrderService.adminOrderDetail(id);
        return BaseRespVo.ok(data);
    }

    /**
     * 发货
     *
     * @param {"orderId":15,"shipChannel":"YTO","shipSn":"888888888888"}
     * @return {"errno":0,"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/21 23:23
     */
    @RequiresPermissions("admin:order:ship")
    @RequestMapping("ship")
    @OperationName("3")
    public BaseRespVo adminOrderShip(@RequestBody OrderShipBo orderShipBo) {
        adminOrderService.adminOrderShip(orderShipBo);
        return BaseRespVo.ok();
    }

    /**
     * delete
     *
     * @param {"orderId":19
     *          orderStatusArray: 101   未付款
     *          orderStatusArray: 102   用户取消
     *          orderStatusArray: 103   系统取消
     *          orderStatusArray: 201   已付款
     *          orderStatusArray: 202   申请退款
     *          orderStatusArray: 203   已退款
     *          orderStatusArray: 301  已发货
     *          orderStatusArray: 401 用户收货
     *          orderStatusArray: 402 系统收货
     * @return {"errno":623,"errmsg":"订单不能删除"}
     *          {"errno":0,"errmsg":"成功"}orderStatusArray: 101
     * @author silic xia
     * @since 2022/11/22 0:04
     */
    @RequiresPermissions("admin:order:delete")
    @RequestMapping("delete")
    @OperationName("4")
    public BaseRespVo adminOrderDelete(@RequestBody OrderIdBo orderIdBo) {
            int code = adminOrderService.adminOrderDelete(orderIdBo.getOrderId());
            if (code == Count.ORDER_DELETE_ERROR){
                return BaseRespVo.fail("订单不能删除", Count.ORDER_DELETE_ERROR);
            }
            return BaseRespVo.ok();
    }
    
    /**
     *  退款refund
     *
     * @param {"orderId":15,"refundMoney":101}
     * @return 
     * @author  silic xia
     * @since 2022/11/22 10:38 
     */
    @RequestMapping("refund")
    @OperationName("2")
    public BaseRespVo adminOrderRefund(@RequestBody OrderRefundBo orderRefundBo){
        int code = adminOrderService.adminOrderRefund(orderRefundBo);
        if (code == Count.ORDER_REFUND_ERROR){
            return BaseRespVo.fail("订单不能确认收货",Count.ORDER_REFUND_ERROR);
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("reply")
    public BaseRespVo reply(@RequestBody AdminOrderReplyBO adminOrderReplyBO) {
        boolean haveComment=adminOrderService.AdminOrderReplyComment(adminOrderReplyBO);
        if (haveComment){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail("订单商品已回复",622);
        }

    }
}

